﻿using BJ.AuthDomain.IService;
using Mapster;
using BJ.AuthEntity.Models;
using BJ.AuthEntity.ViewModels;
using Syspetro.Core.AppDeal;
using Syspetro.Core.DbAccessor;
using Syspetro.Deploy.FreeSqlDb;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;
using Syspetro.Core.Encrypt;
using Syspetro.Core.CommandBus;

namespace BJ.AuthDomain.Service
{
    public class AdminUserService : EntityBaseService<AdminUser>, IAdminUserService
    {
        private readonly IFreeSql _freeSql;
        readonly IRepository<AdminUser> repository;
        readonly ICommandBus _command;
        public AdminUserService(IFreeSql freeSql, ICommandBus command)
        {
            _freeSql = freeSql;
            _command = command;
            this.repository = new FreeSqlRepository<AdminUser>(freeSql);
        }
        public override IRepository<AdminUser> Repository
        {
            get
            {
                return repository;
            }
        }

        public async Task<Guid> AddUserAsync(VM_User_Add user_Add)
        {
            var pswd = _command.Send(new PswdMd5Command { Pswd = user_Add.Password});
            if (!pswd.Success)
                return Guid.Empty;
            var user = user_Add.Adapt<AdminUser>();
            var auth = new AdminUserAuth
            {
                Account = user_Add.AuthName,
                Password = user_Add.Password,
                Id = user.Id
            };
            Repository.AddTrans(user);
            Repository.AddTrans(auth);
            var i = await Repository.SaveAsync;
            return i > 0 ? user.Id : Guid.Empty;
        }

        public Task<int> AddUsersAsync(List<VM_User_Add> user_Adds)
        {
            foreach (var user_Add in user_Adds)
            {
                var pswd = _command.Send(new PswdMd5Command { Pswd = user_Add.Password,});
                var user = user_Add.Adapt<AdminUser>();
                var auth = new AdminUserAuth
                {
                    Account = user_Add.AuthName,
                    Password = pswd.Get().Data,
                    Id = user.Id
                };
                Repository.AddTrans(user);
                Repository.AddTrans(auth);
            }
            return Repository.SaveAsync;
        }

        public async Task<VM_User> GetUserFull(Guid id)
        {
            var user = await _freeSql.Select<AdminUser>().Include(t => t.Role).Include(t => t.Auth).Where(t => t.Id == id).FirstAsync();
            return user.Adapt<VM_User>();
        }

        public async Task<VM_User> UserSearch(VM_User_Search search)
        {
            AdminUser user = search.SearchType switch
            {
                AuthEntity.SearchType.Email => await _freeSql.Select<AdminUser>().Include(t => t.Role).Where(t => t.Emil == search.Name).FirstAsync(),
                AuthEntity.SearchType.SMS => await _freeSql.Select<AdminUser>().Include(t => t.Role).Where(t => t.Phone == search.Name).FirstAsync(),
                _ => await _freeSql.Select<AdminUser>().Include(t => t.Role).Include(t => t.Auth).Where(t => t.Auth.Account == search.Name).FirstAsync(),
            };
            return user.Adapt<VM_User>();
        }
        public async Task<List<VM_User>> GetAdminList(Guid? roleId)
        {
            var list = await _freeSql.Select<AdminUser>().Include(t => t.Role).Include(t => t.Auth).WhereIf(Guid.Empty!=roleId&&roleId!=null,d=>d.RoleId==roleId).ToListAsync();
            
            return list.Adapt<List<VM_User>>();
        }

        public async Task<PageList<VM_User>> FindUsersFull(VM_User_PageReq pageReq)
        {
            bool IsAscending = string.Compare(pageReq.Sorting, "asc", true) == 0;

            var users = await _freeSql.Select<AdminUser>()
                .Include(t => t.Role)
                .Where(pageReq.GetWhereExpression())
                .OrderByPropertyName(pageReq.OrderBy, pageReq.IsAscending())
                .Count(out var _total)
                .Page(pageReq.Current, pageReq.Size)
                .ToListAsync();
            return new PageList<VM_User>
            {
                Size = pageReq.Size,
                Current = pageReq.Current,
                Total = _total,
                Records = users.Adapt<List<VM_User>>()
            };
        }

        public Task<bool> Verify(string account)
        {
            return _freeSql.Select<AdminUserAuth>().AnyAsync(t => t.Account == account);
        }
    }
}
